package org.jivesoftware.spark.crowd.ui;

import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JPopupMenu;

import org.jivesoftware.spark.ChatManager;
import org.jivesoftware.spark.SparkManager;
import org.jivesoftware.spark.component.panes.CollapsibleTitlePane;
import org.jivesoftware.spark.crowd.CrowdRecorder;
import org.jivesoftware.spark.crowd.CrowdsManager;
import org.jivesoftware.spark.crowd.Param;
import org.jivesoftware.spark.crowd.impl.Crowd;
import org.jivesoftware.spark.ui.ChatContainer;
import org.jivesoftware.spark.ui.ChatRoom;
import org.jivesoftware.spark.ui.ChatRoomListener;
import org.jivesoftware.spark.ui.ChatRoomNotFoundException;
/**
 * represents a  crowd on the tab panel.
 * A popup menu is added which can popup by right click your mouse and when you double click your mouse' left
 * button,a crowdChat window will be opened. 
 * @author liyuan
 *
 */
public class CrowdItem extends CollapsibleTitlePane{
	/**
	 * 
	 */
	private static final long serialVersionUID = 98765L;
	private Icon preIcon;
	private String crowdName;
	private JPopupMenu pop;
	
	
	private UIEventManager uiEventManager;
	public CrowdItem(String name,Icon icon){
		super();
		this.crowdName = name;
		setTitle(name);
		this.preIcon = icon;
		this.getPreIconLabel().setIcon(preIcon);
		
		this.uiEventManager = CrowdsManager.getInstance().getUIEventManager();
	}
	/**
	 * add listener
	 */
	public void initialize(){
		createPop();
		final String labelName = crowdName;
		addMouseListener(new MouseAdapter(){
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2 && e.getButton() == 1){										
					uiEventManager.crowdLabelDoubleClick(labelName);
				}else if(e.getButton() == 3){				
					pop.show(e.getComponent(),e.getX(),e.getY());
				}				
			}			
		});
		Crowd c = CrowdsManager.getInstance().findCrowdByName(crowdName);
		
		String toolTipText = "创建人:" + c.getCreator().getNickName() + " 创建时间:" + c.getCreateTime();
		
		this.setToolTipText(toolTipText);
	}

	
	
	private void searchCrowds(){
		//TODO search crowds
	}
	
	private void quitCrowd(String crowdName) {
		// TODO Auto-generated method stub
		
	}		
	private void createPop(){
		pop = new JPopupMenu();
		Action sendAction = new AbstractAction(){
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void actionPerformed(ActionEvent e) {
				uiEventManager.crowdLabelDoubleClick(crowdName);
				
			}			
		};
		sendAction.putValue(Action.NAME, Param.getLabelText("crowdChat","发送群消息"));
		
		Action infoAction = new AbstractAction(){
			private static final long serialVersionUID = 9L;
			@Override
			public void actionPerformed(ActionEvent e) {
				uiEventManager.showCrowdInfo(crowdName);
				
			}
			
		};
		infoAction.putValue(Action.NAME, Param.getLabelText("crowdInfo", "查看群简介"));
		
		Action destroyAction = new AbstractAction(){
			private static final long serialVersionUID = 8L;
			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(){
					@Override
					public void run() {						
						super.run();
						uiEventManager.dismissCrowd(crowdName);
					}				
				}.start();								
			}			
		};
		destroyAction.putValue(Action.NAME, Param.getLabelText("destroyCrowd", "移除这个群"));
		
		Action quitAction = new AbstractAction(){
			private static final long serialVersionUID = 7L;
			@Override
			public void actionPerformed(ActionEvent e) {
				new Thread(new Runnable(){
					@Override
					public void run() {
						uiEventManager.giveUPMembership(crowdName);						
					}				
				}).start();
				quitCrowd(crowdName);
				
			}

				
		};
		quitAction.putValue(Action.NAME, Param.getLabelText("leaveCrowd", "退出该群"));
		pop.add(sendAction);
		pop.add(infoAction);
		
		pop.addSeparator();
		
		Crowd c = CrowdsManager.getInstance().findCrowdByName(crowdName);
		
		if(c != null){
			String creatorJID = c.getCreator().getJID();
			if(creatorJID.equals(Param.getBareAddress()))
				pop.add(destroyAction);
			else 
				pop.add(quitAction);
				
		}
		
		
		
		
	}
	
}
